package graphics;

public class Bitmap2D {

	
public native void mirror(byte axis);
public native void movie(byte xVector,byte yVector,byte zVector);	


private native void newBitmap2D(int xSize,int ySize);

public short data[] [];

private int xSize;
private int ySize;

public static final int AXIS_X		 = 0; 
public static final int AXIS_Y		 = 1;



public Bitmap2D(int xSize,int ySize)
{
	data= new short[ySize] [xSize];
	this.xSize=xSize;
	this.ySize=ySize;
}



public int getXSize()
{
	return(xSize);
}


public int getYSize()
{
	return(ySize);
}






public void fill(short color)
{
	int xCounter;
	int yCounter;
	
	for (yCounter=0;yCounter<ySize;yCounter++)
		for (xCounter=0;xCounter<xSize;xCounter++)
			data[yCounter] [xCounter] =color;
}






public void setPixel(int xPos,int yPos,short color)
{
	if ((xPos<data[0].length) && (yPos<data.length))
		data[yPos] [xPos] =color;
}





public short getPixel(int xPos,int yPos)
{
	if ((xPos<data[0].length) && (yPos<data.length))
		return(data[yPos] [xPos]);
	
	return(0);
}



public void insert(int xPos,int yPos,Bitmap2D source)
{
	
	int xCounter;
	int yCounter;
	
	
	for (yCounter=0;yCounter<ySize;yCounter++)
		for (xCounter=0;xCounter<xSize;xCounter++)
		{
			
		}
	
}




public void mirror(int axis)
{
	switch  (axis)
	{
	case AXIS_X:
		mirrorX();
		break;
		
	case AXIS_Y:
		mirrorY();
		break;
		
	}
}


/**
 * @author Tavin
 * 
 * Date 2011.05.19
 * 
 * mirror bitmap over x axis 
 * 
 */

private void mirrorX()
{
	int xCounter;
	int yCounter;
	short tmpValue;
	
	for (yCounter=0;yCounter<ySize;yCounter++)
		for (xCounter=0;xCounter<xSize/2;xCounter++)
			{
			tmpValue=data[yCounter] [xCounter];
			data[yCounter] [xCounter]=data[yCounter] [xSize-xCounter-1];
			data[yCounter] [xSize-xCounter-1]=tmpValue;
			}
}



/**
 * @author Tavin
 * 
 * Date 2011.05.19
 * 
 * mirror bitmap over y axis 
 * 
 */

private void mirrorY()
{
	int xCounter;
	int yCounter;
	short tmpValue;
	

	for (xCounter=0;xCounter<xSize;xCounter++)
		for (yCounter=0;yCounter<ySize/2;yCounter++)
		{
			tmpValue=data[yCounter] [xCounter];
		
			data[yCounter] [xCounter]=data[ySize-yCounter-1] [xCounter];
			data[ySize-yCounter-1] [xCounter]=tmpValue;
		}	
}

/**
 * @author Tavin
 * 
 * Date 2011.05.19
 * 
 * 
 * 
 */


public void not()
{
	int xCounter;
	int yCounter;
	
	for (yCounter=0;yCounter<ySize;yCounter++)
		for (xCounter=0;xCounter<xSize;xCounter++)
			data[yCounter] [xCounter] =(short)( ~ (data[yCounter] [xCounter]));	//_not(data);
}


public void max(Bitmap2D source)
{
	int xCounter;
	int yCounter;
		
	int color;
	
	for (yCounter=0;((yCounter<ySize) && yCounter<source.getYSize() );yCounter++)
		for (xCounter=0;((xCounter<xSize) && xCounter<source.getXSize() );xCounter++)
		{
		color=0;
		
		if ((data[yCounter] [xCounter]&0xf )<(source.data[yCounter] [xCounter]&0xf ))
			{
			color=(source.data[yCounter] [xCounter]&0xf);
			}
			else 
				{
				color=(data[yCounter] [xCounter]&0xf);
				}

		
		if ((data[yCounter] [xCounter]&0xf0 )<(source.data[yCounter] [xCounter]&0xf0 ))
			{
			color|=(source.data[yCounter] [xCounter]&0xf0);
			}
			else 
				{
				color|=(data[yCounter] [xCounter]&0xf0);
				}
		

		if ((data[yCounter] [xCounter]&0xf00 )<(source.data[yCounter] [xCounter]&0xf00 ))
			{
			color|=(source.data[yCounter] [xCounter]&0xf00);
			}
			else 
				{
				color|=(data[yCounter] [xCounter]&0xf00);
				}

		
		
		}
}

public void min(Bitmap2D source)
{
	int xCounter;
	int yCounter;
		
	int color;
	
	for (yCounter=0;((yCounter<ySize) && (yCounter<source.getYSize()));yCounter++)
		for (xCounter=0;((xCounter<xSize) && xCounter<source.getXSize() );xCounter++)
		{
		color=0;
	

			
		if ((data[yCounter] [xCounter]&0xf )>(source.data[yCounter] [xCounter]&0xf ))
			{
			color=(source.data[yCounter] [xCounter]&0xf);
			}
			else 
				{
				color=(data[yCounter] [xCounter]&0xf);
				}

		
		if ((data[yCounter] [xCounter]&0xf0 )>(source.data[yCounter] [xCounter]&0xf0 ))
			{
			color|=(source.data[yCounter] [xCounter]&0xf0);
			}
			else 
				{
				color|=(data[yCounter] [xCounter]&0xf0);
				}
		

		if ((data[yCounter] [xCounter]&0xf00 )>(source.data[yCounter] [xCounter]&0xf00 ))
			{
			color|=(source.data[yCounter] [xCounter]&0xf00);
			}
			else 
				{
				color|=(data[yCounter] [xCounter]&0xf00);
				}		
		
		}
}



public void add(Bitmap2D source)
{

}

public void addS(Bitmap2D source)
{

}

public void sub(Bitmap2D source)
{

}

public void subS(Bitmap2D source)
{

}
/***
 * 
 * @param index
 * @param color
 * @param x
 * @param y
 * @return
 */


public boolean print5x5Char(char index,short color,int x,int y)
{
	int xCounter;
	int yCounter;
	byte dataLine;
	
	index-=32;
	// ToDO murks for Bitmaps	
	if ((x < -4) || (xSize<x) || (y < -4) || (ySize<y))
		return(false);
		
	if (index>=Font5x5.font5x5.length)
		return(false);
	
	for (yCounter=0;yCounter<5;yCounter++)
	{
		dataLine=Font5x5.font5x5[index] [4-yCounter];

		for (xCounter=4;xCounter>=0;xCounter--)
		{	
			if ((dataLine &0x1) >0)
				{
				setPixel(x+ xCounter, y+ yCounter, color);
				}
			dataLine>>=1;
		}
	}
	
	
	return(true);
}




public boolean print8x8Char(char index,short color,int x, int y)
{
	int xCounter;
	int yCounter;
	byte dataLine;
	
	index-=32;

	if ((x < -7) || (xSize<x) || (y < -7) || (ySize<y))
		return(false);
		
	if (index>=Font8x8.font8x8.length)
		return(false);
	
	for (yCounter=0;yCounter<8;yCounter++)
	{
		dataLine=Font8x8.font8x8[index] [7-yCounter];

		for (xCounter=7;xCounter>=0;xCounter--)
		{	
			if ((dataLine &0x1) >0)
				{
				setPixel(x+ xCounter, y+ yCounter, color);
				}
			dataLine>>=1;
		}
	}
	
	
	return(true);



}


}
